/* rmvuxr.p - SUPPLIER RESPONSIBILITY OWNING PROGRAM                          */
/* Copyright 1986-2000 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.7 $                                                           */
/*                                                                            */
/* This routine provides procedures related to Supplier.                      */
/*                                                                            */
/* Revision: 1.1    BY: Poonam Bahl        DATE: 03/03/00   ECO: *N03T*       */
/* Revision: 1.2    BY: Julie Milligan     DATE: 05/04/00   ECO: *N09T*       */
/* Revision: 1.4    BY: Jack Rief          DATE: 06/11/00   ECO: *N0B9*       */
/* Revision: 1.5    BY: Julie Milligan     DATE: 07/20/00   ECO: *N059*       */
/* Revision: 1.6    BY: Larry Leeth        DATE: 12/04/00   ECO: *N0V1*       */
/* $Revision: 1.7 $        BY: B.S. Rajput      DATE: 11/28/00  ECO: *N0WJ*       */
/*                                                                            */
/*V8:ConvertMode=NoConvert                                                    */
{cxcustom.i "rmvuxr.p"}
{mfdeclre.i}
{pxmaint.i}
/* Define Handles for the programs. */
{pxphdef.i adadxr}
{pxphdef.i adarxr}
{pxphdef.i adcrxr}
{pxphdef.i apapxr}
{pxphdef.i apbkxr}
{pxphdef.i aperxr}
{pxphdef.i appmtxr}
{pxphdef.i glacxr}
{pxphdef.i gpcodxr}
{pxphdef.i gplngxr}
{pxphdef.i mcexxr}
{pxphdef.i popoxr}
{pxphdef.i ppplxr}
{pxphdef.i pxtools}
/* End Define Handles for the programs. */


/* ========================================================================== */
/* ******************************** FUNCTIONS ******************************* */
/* ========================================================================== */
/*============================================================================*/
FUNCTION isSupplierPerformanceEnabled RETURNS logical :
/*------------------------------------------------------------------------------
Purpose: Checks to see if Supplier performance is enabled
Exceptions:
Conditions:
        Pre:
        Post:
Notes:
History:
------------------------------------------------------------------------------*/
   define variable returnData as logical no-undo.
     returnData = False.
     if can-find (mfc_ctrl where
                  mfc_field = "enable_supplier_perf" and mfc_logical)
                  and
                  can-find (_File where _File-name = "vef_ctrl") then do:
          returnData = True.
     end.
   return (returnData).
END FUNCTION.
/* ========================================================================== */
/* ========================================================================== */
/* *************************** INTERNAL PROCEDURES ************************** */
/* ========================================================================== */
/*============================================================================*/
PROCEDURE processRead:
/*------------------------------------------------------------------------------
Purpose:       Read the pv_mstr record
Exceptions:    RECORD-NOT-FOUND, RECORD-LOCKED
Dependencies:
   Input-      None
   Output-     pv_mstr(r)
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter  pSupplierId   as  character no-undo.
   define parameter buffer pv_mstr       for pv_mstr.
   define input parameter  pLockRecord   as  logical no-undo.
   define input parameter  pWait         as  logical no-undo.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if pLockRecord then do:
         if pWait then do:
            find first pv_mstr exclusive-lock
               where pv_addr = pSupplierId no-error.
         end. /* if pWait */
         else do:
            find first pv_mstr exclusive-lock
               where pv_addr = pSupplierId no-error no-wait.
            if locked pv_mstr then
               return {&RECORD-LOCKED} .
         end. /* else */
      end. /* if pLockRecord */
      else do:
         for first pv_mstr no-lock
            where pv_addr = pSupplierId:
         end. /* for first pv_mstr no-lock */
      end. /* else */
      if not available pv_mstr then
         return {&RECORD-NOT-FOUND}.
   end. /* do on error undo, return error {&GENERAL-APP-EXCEPT}: */
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE getSupplierCurrency:
/*------------------------------------------------------------------------------
Purpose:       Return the default currency based for the supplier
Exceptions:    RECORD-NOT-FOUND
Dependencies:
   Input-      pv_mstr(r)
   Output-     pv_mstr(r)
Notes:
History:
------------------------------------------------------------------------------*/
   define input  parameter pSupplierId as character no-undo.
   define output parameter pCurr       as character no-undo.
   define buffer pv_mstr for pv_mstr.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      for first pv_mstr fields(pv_addr pv_curr) no-lock
         where pv_addr = pSupplierId:
         pCurr = pv_curr.
      end. /* for first pv_mstr */
      if not available pv_mstr then
         return {&RECORD-NOT-FOUND}.
   end. /* do on error undo, return error {&GENERAL-APP-EXCEPT}: */
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE validateSupplierId:
   /*---------------------------------------------------------------------------
   Purpose: TO VALIDATE SUPPLIER ID.
   Exceptions: APP-ERROR-RESULT
   Notes:
   History:
   ---------------------------------------------------------------------------*/
   define input parameter pSupplierId as character no-undo.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if not can-find(pv_mstr
         where pv_addr = pSupplierId)
           and pSupplierId <> "" then do:
         /* NOT A VALID SUPPLIER */
         {pxmsg.i &MSGNUM     = 2
                  &ERRORLEVEL = {&APP-ERROR-RESULT}}
         return error {&APP-ERROR-RESULT}.
      end. /* if not can-find(pv_mstr */
   end. /* do on error undo, return error {&GENERAL-APP-EXCEPT}: */
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* validateSupplierId */
/*============================================================================*/
PROCEDURE getPurchaseAccountData :
/*------------------------------------------------------------------------------
Purpose:       Get Purchase Accounts for the Supplier
Exceptions:    NONE
Conditions:
        Pre:   NONE
        Post:  pv_mstr(r)
Notes:
History:       Extracted from popomta.p
------------------------------------------------------------------------------*/
   define input parameter  pSupplierId    as character no-undo.
   define output parameter pPurAccount    as character no-undo.
   define output parameter pPurSubAccount as character no-undo.
   define output parameter pPurCostCenter as character no-undo.
   define buffer pv_mstr for pv_mstr.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      for first pv_mstr fields (pv_addr pv_pur_acct pv_pur_sub pv_pur_cc)
         where pv_addr = pSupplierId no-lock:
      end.
         assign
            pPurAccount    = pv_mstr.pv_pur_acct
            pPurSubAccount = pv_pur_sub
            pPurCostCenter = pv_pur_cc.
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE setModificationInfo  :
/*------------------------------------------------------------------------------
Purpose:       Set auditing informtaion in supplier buffer .
Exceptions:    NONE
Conditions:
        Pre:   NONE
        Post:  pv_mstr(r)
Notes:
History:       Extracted from adrtmt.p
------------------------------------------------------------------------------*/
   define parameter buffer pSupplierBuffer for pv_mstr .
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      /* STORE MODIFY DATE AND USERID */
      assign
        pv_mod_date = today
         pv_userid   = global_userid.
   end. /* do on error undo, return error {&GENERAL-APP-EXCEPT}: */
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE setRemitToReference  :
/*------------------------------------------------------------------------------
Purpose:       Set remit to reference in supplier buffer .
Exceptions:    NONE
Conditions:
        Pre:   NONE
        Post:  pv_mstr(r)
Notes:
History:       Extracted from adrtmt.p
------------------------------------------------------------------------------*/
   define input parameter pSupplierId as character no-undo .
   define input parameter pReference  as character no-undo .
   define buffer bVdMstr for pv_mstr.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      {pxrun.i &PROC='processRead'
         &PARAM="(input pSupplierId,
                  buffer bVdMstr,
                  input {&LOCK_FLAG},
                  input {&NO_WAIT_FLAG}
                 )"
      }
      if return-value = {&SUCCESS-RESULT} then  do:
         assign
           bVdMstr.pv_remit = pReference .
         {pxrun.i &PROC='setModificationInfo'
            &PARAM="(buffer bVdMstr)"
         }
      end.
   end. /* do on error undo, return error {&GENERAL-APP-EXCEPT}: */
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE checkForPO  :
/*------------------------------------------------------------------------------
Purpose:       Check whether purchase Orders exist for the supplier
Exceptions:    NONE
Conditions:
        Pre:   NONE
        Post:  pv_mstr(r)
Notes:
History:       Extracted from adrtmt.p
------------------------------------------------------------------------------*/
   define input parameter pSupplierId as character no-undo .
   define buffer pPoMstr for po_mstr.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      for first pPoMstr
         where po_ship = pSupplierId
         no-lock :
      end.
      if available pPoMstr then do :
         /* MESSAGE #221 - DELETE NOT ALLOWED, OUTSTANDING PURCHASE ORDER */
         {pxmsg.i
            &MSGNUM=221
            &ERRORLEVEL={&APP-ERROR-RESULT}
            &MSGARG1=po_nbr}
         return error {&APP-ERROR-RESULT}.
      end.
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE createSupplier :
/*------------------------------------------------------------------------------
Purpose:  Creates a supplier
Exceptions:    NONE
Conditions:
        Pre:
        Post: pv_mstr(c)
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pSupplierId as character no-undo.
   define  parameter buffer bSupplier for pv_mstr.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      create bSupplier.
      assign pv_addr = pSupplierId.
      if recid(bSupplier) = -1 then .
      {pxrun.i &PROC='setNewRecord' &PROGRAM='pxtools.p'
                &HANDLE=ph_pxtools
               &PARAM="(input buffer bSupplier:handle, yes)"}
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE processCreate :
/*------------------------------------------------------------------------------
Purpose:  Creates a supplier
Exceptions:    NONE
Conditions:
        Pre:
        Post: pv_mstr(c)
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pSupplierId as character no-undo.
   define parameter buffer bSupplier for pv_mstr.
   define buffer bLsMstr for ls_mstr.
   define buffer bERSMstr for ers_mstr.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      {pxrun.i &PROC='createSupplier'
               &PARAM="(input pSupplierId,
         buffer bSupplier)"
      }
      {pxgetph.i aperxr}

      if {pxfunct.i &FUNCTION='isERSEnabled' &PROGRAM='aperxr.p'
                     &HANDLE=ph_aperxr
                    &PARAM ="input pSupplierId"} then do:
           {pxrun.i &PROC='createERSSupplier' &PROGRAM='aperxr.p'
                     &HANDLE=ph_aperxr
                    &PARAM="(buffer bERSMstr,
                             input pSupplierId)"
           }
      end.
      {pxrun.i &PROC='setDefaultAccountFields'
               &PARAM="(buffer bSupplier)"
      }
      {pxrun.i &PROC='setDefaultAccountsPayableFields'
               &PARAM="(buffer bSupplier)"
      }
      {pxrun.i &PROC='processCreate' &PROGRAM='adarxr.p'
                &HANDLE=ph_adarxr
               &PARAM="(input  bSupplier.pv_addr,
                        input 'supplier',
                        buffer bLsMstr)"
      }
      {pxrun.i &PROC='setModificationInfo'
               &PARAM="(buffer bSupplier)"
      }
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE deleteSupplier :
/*------------------------------------------------------------------------------
Purpose:  Deletes a supplier
Exceptions:    NONE
Conditions:
        Pre:
        Post: pv_mstr(d)
Notes:
History:
------------------------------------------------------------------------------*/
   define  parameter buffer bSupplier for pv_mstr.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
        delete bSupplier.
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE setDefaultAccountFields :
/*------------------------------------------------------------------------------
Purpose:  Sets the vendor AP/PO account from the GL Control files and
          also the default Vendor currency
Exceptions:    NONE
Conditions:
        Pre: pv_mstr(r)
        Post:
Notes:
History:
------------------------------------------------------------------------------*/
   define  parameter buffer bSupplier for pv_mstr.
   define buffer bGlCtrl for gl_ctrl.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      find first bGlCtrl no-lock no-error.
      if available bGlCtrl then do:
         assign pv_ap_acct = gl_ap_acct
                pv_ap_sub = gl_ap_sub
                pv_ap_cc = gl_ap_cc
                pv_pur_acct = gl_pur_acct
                pv_pur_sub = gl_pur_sub
                pv_pur_cc = gl_pur_cc
                pv_curr = gl_base_cur.
      end.
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE setDefaultAccountsPayableFields :
/*------------------------------------------------------------------------------
Purpose: Sets basic default AP information  (ie:Bank, checkForm )
Exceptions:    NONE
Conditions:
        Pre: pv_mstr(r)
        Post:
Notes:
History:
------------------------------------------------------------------------------*/
   define  parameter buffer bSupplier for pv_mstr.
   define buffer bApcCtrl for apc_ctrl.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      find first bApcCtrl no-lock no-error.
      if available bApcCtrl then do:
        assign pv_bank  = apc_bank
               pv_ckfrm = apc_ckfrm.
      end.
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE getAutoEMT :
/*------------------------------------------------------------------------------
Purpose: Returns the Auto EMT status
Exceptions:    NONE
Conditions:
        Pre:
        Post:
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pSupplierId as character no-undo.
   define output parameter pEMTAuto as logical no-undo.
   define buffer bQadWkfl for qad_wkfl.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
       pEMTAuto = if can-find(bQadWkfl where qad_key1 = pSupplierId and
                                             qad_key2 = "AUTO-EMT")
                  then yes
              else no.
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE getDBNumber :
/*------------------------------------------------------------------------------
Purpose: Returns the  Dun & Brad Street number
Exceptions:    NONE
Conditions:
        Pre:
        Post:
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pSupplierId as character no-undo.
   define output parameter pDBNbr as character no-undo.
   define buffer bQadWkfl for qad_wkfl.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
     for first bQadWkfl where qad_key1 = "pv_mstr_ve"  and
                              qad_key2 = pSupplierId no-lock:
     end.
     if available (bQadWkfl) then
         assign pDBNbr = qad_charfld[2].
     else
         assign pDBNbr = "".
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE getTIDNotice :
/*------------------------------------------------------------------------------
Purpose: Returns the TID Notice
Exceptions:    NONE
Conditions:
        Pre:
        Post:
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pSupplierId as character no-undo.
   define output parameter pTIDNotice as character no-undo.
   define buffer bQadWkfl for qad_wkfl.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
     for first bQadWkfl where qad_key1 = "advnmt.p"
                        and   qad_key2 = pSupplierId
                  no-lock :
     end.
     if available (bQadWkfl) then
        assign pTIDNotice = qad_charfld[1].
     else
        assign pTIDNotice = "".
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE setAutoEMT :
/*-----------------------------------------------------------------------------
Purpose: Sets the Auto EMT status for the supplier
Exceptions:    NONE
Conditions:
        Pre:
        Post:
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pSupplierId as character no-undo.
   define input parameter pEMTAuto as logical no-undo.
   define buffer bQadWkfl for qad_wkfl.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if pEMTAuto = yes and not can-find(bQadWkfl where
                   qad_key1 = pSupplierId and
                   qad_key2 = "AUTO-EMT")
      then do:
         create bQadWkfl.
         assign qad_key1 = pSupplierId
                qad_key2 = "AUTO-EMT".
      end. /* then do: */
      if pEMTAuto = no then do:
         find first bQadWkfl where qad_key1 = pSupplierId and
                                   qad_key2 = "AUTO-EMT" exclusive-lock
                                                         no-error.
         if available bQadWkfl then delete bQadWkfl.
      end. /* if pEMTAuto = no then do: */
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE setDBNumber :
/*------------------------------------------------------------------------------
Purpose: Sets the Dun & Brad Street number for the supplier
Exceptions:    NONE
Conditions:
        Pre:
        Post:
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pSupplierId as character no-undo.
   define input parameter pDBNbr as character no-undo.
   define buffer bQadWkfl for qad_wkfl.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
         for first bQadWkfl where
               qad_key1 = "pv_mstr_ve" and
               qad_key2 = pSupplierId exclusive-lock:
         end.
         if not available (bQadWkfl) then do:
        if pDBNbr <> "" then do:
                create bQadWkfl.
               assign
               qad_key1 = "pv_mstr_ve"
               qad_key2 = pSupplierId
               qad_charfld[2] = pDBNbr.
               if recid(bQadWkfl) = -1 then .
             end.
         end.
         else do:
            if qad_charfld[2] <> pDBNbr then
               qad_charfld[2] = pDBNbr.
         end.
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE setMiscCreditorDefaults :
/*------------------------------------------------------------------------------
Purpose: Sets the default Miscellaneous credit information
Exceptions:    NONE
Conditions:
        Pre:  pv_mstr (r)
        Post:
Notes:
History:
------------------------------------------------------------------------------*/
   define  parameter buffer bSupplier for pv_mstr.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if pv_misc_cr then
          assign pv_cr_terms = ""
                 pv_1099 = no.
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE setTIDNotice :
/*------------------------------------------------------------------------------
Purpose: Sets the TID information for the supplier
Exceptions:    NONE
Conditions:
        Pre:
        Post:
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pSupplierId as character no-undo.
   define input parameter pTIDNotice as character no-undo.
   define buffer bQadWkfl for qad_wkfl.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
         for first bQadWkfl where
               qad_key1 = "advnmt.p" and
               qad_key2 = pSupplierId exclusive-lock:
         end.
         if not available (bQadWkfl) then do:
            if pTIDNotice <> "" then do:
               create bQadWkfl.
               assign
               qad_key1 = "advnmt.p"
               qad_key2 = pSupplierId
               qad_charfld[1] = pTIDNotice.
               if recid(bQadWkfl) = -1 then .
             end.
         end.
         else do:
            if qad_charfld[1] <> pTIDNotice then
               qad_charfld[1] = pTIDNotice.
         end.
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE deleteWorkFiles :
/*------------------------------------------------------------------------------
Purpose: Deletes the TID, E-mail, Dun & Brad number and Auto EMT information
    for the supplier
Exceptions:    NONE
Conditions:
        Pre:
        Post:
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pSupplierId as character no-undo.
   define buffer bQadWkfl for qad_wkfl.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
         for first bQadWkfl where
               qad_key1 = "advnmt.p" and
               qad_key2 = pSupplierId exclusive-lock:
         end.
         /* delete TID notice */
         if available (bQadWkfl) then do:
            delete bQadWkfl.
         end.
         for first bQadWkfl where
               qad_key1 = pSupplierID and
               qad_key2 = "AUTO-EMT" exclusive-lock:
         end.
         /* delete AUTO-EMT */
         if available (bQadWkfl) then do:
            delete bQadWkfl.
         end.
         for first bQadWkfl where
               qad_key1 = "pv_mstr_ve" and
               qad_key2 = pSupplierId exclusive-lock:
         end.
         /* delete DBNbr & E-mail */
         if available (bQadWkfl) then do:
            delete bQadWkfl.
         end.
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE validateAccountCurrency :
/*------------------------------------------------------------------------------
Purpose:  Validates the account currency will also pass the field name
     along with the message if an error occurs
Exceptions:    NONE
Conditions:
        Pre:
        Post:
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pAccount as character no-undo.
   define input parameter pSupplierCurrency as character no-undo.
   define input parameter pBaseCurrency as character no-undo.
   define input parameter pFieldName as character no-undo.
   define variable vAccountCurrency like ac_curr no-undo.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      {pxrun.i &PROC='getAccountCurrency' &PROGRAM='glacxr.p'
                &HANDLE=ph_glacxr
               &PARAM="(input pAccount,
                        output vAccountCurrency)"
      }
      if return-value = {&SUCCESS-RESULT} and
         vAccountCurrency <> pSupplierCurrency and
         vAccountCurrency <> pBaseCurrency then do:
         /* ACCOUNT CURRENCY <> EITHER SUPPLIER OR BASE CURRENCY */
         {pxmsg.i &MSGNUM=4092
                  &ERRORLEVEL={&WARNING-RESULT}
                  &FIELDNAME=pFieldName
         }
         return {&WARNING-RESULT} .
      end.
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE validateBank :
/*------------------------------------------------------------------------------
Purpose:  Validates the Bank ID and the currency against teh supplier Currency
Exceptions:    NONE
Conditions:
        Pre:
        Post:
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pBankCode as character no-undo.
   define input parameter pSupplierCurrency as character no-undo.
   define input parameter pBaseCurrency as character no-undo.
   define variable vBankCurrency as character no-undo.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if pBankCode <> "" then do:
           {pxrun.i &PROC='validateBankId' &PROGRAM='apbkxr.p'
                     &HANDLE=ph_apbkxr
                    &PARAM="(input pBankCode)"
           }
           {pxrun.i &PROC='getBankCurrency' &PROGRAM='apbkxr.p'
                     &HANDLE=ph_apbkxr
                    &PARAM="(input pBankCode,
                             output vBankCurrency)"
           }
           if vBankCurrency <> pSupplierCurrency and
                 vBankCurrency <> pBaseCurrency then do:
             /* BANK DEFINED FOR DIFFERENT CURRENCY */
             {pxmsg.i &MSGNUM=93
                      &ERRORLEVEL={&WARNING-RESULT}
             }
             return {&WARNING-RESULT} .
           end.
      end. /* if pBankCode <> "" then do: */
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE validateMiscCreditorCheckForm :
/*------------------------------------------------------------------------------
Purpose:  validates the check forms for miscellaneous creditors
Exceptions:    NONE
Conditions:
        Pre:
        Post:
Notes:
History:
------------------------------------------------------------------------------*/
   {&rmvuxr-P-TAG1}
   define  input parameter pCheckForm as character no-undo.
   define  input parameter pMiscCreditor as logical no-undo.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      {&rmvuxr-P-TAG2}
      if pMiscCreditor and lookup(pCheckForm, "1,2") = 0 then do:
         /* CHECK FORM INVALID FOR MISC CREDITORS */
         {pxmsg.i &MSGNUM=2188
                  &ERRORLEVEL={&APP-ERROR-RESULT}
         }
         {&rmvuxr-P-TAG3}
         return error {&APP-ERROR-RESULT} .
      end. /* if pv_misc_cr and lookup(pv_ckfrm, "1,2") = 0 then do: */
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE validatePriceReduction :
/*------------------------------------------------------------------------------
Purpose: Validates the price reduction against the sales price
Exceptions:    NONE
Conditions:
        Pre:
        Post:
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter bTransferPrice as decimal no-undo.
   define input parameter bUseTransferPrice as logical no-undo.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if bTransferPrice = 0 and bUseTransferPrice then do:
         /* PURCHASE PRICE IS EQUAL TO THE SALES PRICE */
         {pxmsg.i &MSGNUM=2832
                  &ERRORLEVEL={&WARNING-RESULT}
         }
         return {&WARNING-RESULT} .
      end. /* if bTransferPrice = 0 and bUseTransferPrice then do: */
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE validatePriceReductionLimit :
/*------------------------------------------------------------------------------
Purpose: Validates the price reduction against the maximum of 100 %
Exceptions:    NONE
Conditions:
        Pre:
        Post:
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter bTransferPrice as decimal no-undo.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if bTransferPrice > 100 then do:
         /* VALUE CANNOT BE GREATER THAN 100% */
         {pxmsg.i &MSGNUM=2803
                  &ERRORLEVEL={&APP-ERROR-RESULT}
         }
         return error {&APP-ERROR-RESULT}.
      end.
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE validatePromotionGroup :
/*------------------------------------------------------------------------------
Purpose:  Validates the promotion group in the generalized codes
Exceptions:    NONE
Conditions:
        Pre:
        Post:
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pVdPromo as character no-undo.
   do on error undo, return error return-value:
      {pxrun.i &PROC='validateGeneralizedCodes' &PROGRAM='gpcodxr.p'
                &HANDLE=ph_gpcodxr
         &PARAM="(input 'pv_promo' ,
                  input '' ,
                  input pVdPromo ,
                  input ''
                 )"
         &FIELD-LIST=pv_promo
      }
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE getEmailID :
/*------------------------------------------------------------------------------
Returns the E-mail ID of the supplier
rmvuxr.p
getEmailID (
   input character pSupplierId)
Parameters:
   pSupplierId -
Exceptions:
PreConditions:
PostConditions:
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pSupplierId as character no-undo.
   define output parameter pSupplierEmailId as character no-undo.
   define buffer bQadWkfl for qad_wkfl.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
           for first bQadWkfl where
               qad_key1 = "pv_mstr_ve"  and
               qad_key2 = pSupplierId no-lock:
           end.
           if available (bQadWkfl) then
              assign
                  pSupplierEmailId  = qad_charfld[1].
           else
              assign
                  pSupplierEmailId  = "".
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE setEmailID :
/*------------------------------------------------------------------------------
Sets the E-mail ID for the supplier
rmvuxr.p
setEmailID (
   input character pSupplierId,
   input character pEmailID)
Parameters:
   pSupplierId -
   pEmailID -
Exceptions:
PreConditions:
PostConditions:
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pSupplierId as character no-undo.
   define input parameter pEmailID as character no-undo.
   define buffer bQadWkfl for qad_wkfl.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
               for first bQadWkfl where
                    qad_key1 = "pv_mstr_ve" and
                    qad_key2 = pSupplierId exclusive-lock:
               end.
              if not available (bQadWkfl) then do:
                if pEmailID <> "" then do:
                 create bQadWkfl.
                 assign
                   qad_key1 = "pv_mstr_ve"
                   qad_key2 = pSupplierId
                   qad_charfld[1] = pEmailID.
                   if recid(bQadWkfl) = -1 then .
                end.
              end.
              else do:
                if qad_charfld[1] <> pEmailID then
                   qad_charfld[1] = pEmailID.
              end. /* else do */
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE validateDelete:
/*------------------------------------------------------------------------------
Validates the supplier for Delete
rmvuxr.p
setEmailID (
   input character pSupplierId,
   input character pEmailID)
Parameters:
   pSupplierId -
   pEmailID -
Exceptions:
PreConditions:
PostConditions:
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pSupplierId as character no-undo.
   do on error undo, return error return-value:
         /* Begin delete validations. */
         {pxrun.i &PROC='CheckForOpenSupplierPO' &PROGRAM='popoxr.p'
                   &HANDLE=ph_popoxr
                  &PARAM="(input pSupplierId)"
         }
         {pxrun.i &PROC='CheckForOpenSupplierRecords' &PROGRAM='apapxr.p'
                   &HANDLE=ph_apapxr
                  &PARAM="(input pSupplierId)"
         }
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE processDelete :
/*---------------------------------------------------------------------------
  Purpose:     Delete Supplier
  Exceptions:  NONE
  Notes:
  History:
---------------------------------------------------------------------------*/
define parameter buffer bSupplier for pv_mstr.
define buffer bAddressRole for ls_mstr.
 do on error undo, return error return-value:
   {pxrun.i &PROC='processRead' &PROGRAM='adarxr.p'
             &HANDLE=ph_adarxr
            &PARAM="(input bSupplier.pv_addr,
                     input 'supplier',
                     buffer bAddressRole,
                     input {&LOCK_FLAG},
                     input {&WAIT_FLAG})"
   }
   {pxrun.i &PROC='processDelete' &PROGRAM='adarxr.p'
             &HANDLE=ph_adarxr
            &PARAM="(buffer bAddressRole)"
   }
   {pxrun.i &PROC='deleteERSSupplier' &PROGRAM='aperxr.p'
             &HANDLE=ph_aperxr
            &PARAM="(input bSupplier.pv_addr)"
   }
   {pxrun.i &PROC='deleteWorkFiles'
            &PARAM="(input bSupplier.pv_addr)"
   }
   {pxrun.i &PROC='deleteSupplier'
            &PARAM="(buffer bSupplier)"
   }
 end.
 return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE validateWrite :
/*------------------------------------------------------------------------------
  Purpose:     Validate Supplier Buffer
  Exceptions:  Whatever is returned from the individual run statements.
  Notes:       This procedure is called by the Supplier data type controller to
               perform the business & schema validations for the pv_mstr
               fields displayed in the Supplier Maintenance application.
  History:
------------------------------------------------------------------------------*/
   define parameter buffer bSupplier for pv_mstr.
   define input parameter pBaseCurr as character no-undo.
   /* Constants used for processing price lists. */
   {ppprlst.i}
   define variable invalidListFlag as logical no-undo.
   define variable messageNumber as integer no-undo.
   do on error undo, return error return-value:
      /* business validations */
    {pxrun.i &PROC='validateType'
              &PARAM="(input bSupplier.pv_type)"
              &FIELD-LIST=pv_type
     }
    {pxrun.i &PROC='validateShipVia'
              &PARAM="(input bSupplier.pv_shipvia)"
              &FIELD-LIST=pv_shipvia
     }
    {pxrun.i &PROC='validateLanguage'
              &PARAM="(input bSupplier.pv_lang)"
              &FIELD-LIST=pv_lang
     }
    {pxrun.i &PROC='validateBuyer'
              &PARAM="(input bSupplier.pv_buyer)"
              &FIELD-LIST=pv_buyer
     }
    {pxrun.i &PROC='validateUser1'
              &PARAM="(input bSupplier.pv_user1)"
              &FIELD-LIST=pv_user1
     }
    {pxrun.i &PROC='validateUser2'
              &PARAM="(input bSupplier.pv_user2)"
              &FIELD-LIST=pv_user2
     }
     {pxrun.i &PROC='validateAccount' &PROGRAM='glacxr.p'
               &HANDLE=ph_glacxr
              &PARAM="(input bSupplier.pv_ap_acct,
                       input bSupplier.pv_ap_sub,
                       input bSupplier.pv_ap_cc)"
              &FIELD-LIST=pv_ap_acct
     }
     {pxrun.i &PROC='validateAccount' &PROGRAM='glacxr.p'
               &HANDLE=ph_glacxr
              &PARAM="(input bSupplier.pv_pur_acct,
                       input bSupplier.pv_pur_sub,
                       input bSupplier.pv_pur_cc)"
              &FIELD-LIST=pv_pur_acct
     }
     {pxrun.i &PROC='validateAccountCurrency'
              &PARAM="(input bSupplier.pv_ap_acct,
                       input bSupplier.pv_curr,
                       input pBaseCurr,
                       input 'pv_ap_acct')"
     }
     {pxrun.i &PROC='validateAccountCurrency'
              &PARAM="(input bSupplier.pv_pur_acct,
                       input bSupplier.pv_curr,
                       input pBaseCurr,
                       input 'pv_pur_acct')"
     }
     {pxrun.i &PROC='validatePromotionGroup'
              &PARAM="(input bSupplier.pv_promo)"
     }
     {pxrun.i &PROC='validateCurrency' &PROGRAM='mcexxr.p'
               &HANDLE=ph_mcexxr
              &PARAM="(input bSupplier.pv_curr)"
              &FIELD-LIST=pv_curr
     }
     {pxrun.i &PROC='validateBank'
              &PARAM="(input bSupplier.pv_bank,
                       input bSupplier.pv_curr,
                       input pBaseCurr)"
     }
     {pxrun.i &PROC='validateCheckForm' &PROGRAM='appmtxr.p'
               &HANDLE=ph_appmtxr
              &PARAM="(input bSupplier.pv_ckfrm)"
              &FIELD-LIST=pv_ckfrm
     }
     {pxrun.i &PROC='validateCheckFormCurrency' &PROGRAM='appmtxr.p'
               &HANDLE=ph_appmtxr
              &PARAM="(input bSupplier.pv_ckfrm,
                       input bSupplier.pv_curr,
                       input pBaseCurr,
             input {&WARNING-RESULT})"
              &FIELD-LIST=pv_ckfrm
     }
     {pxrun.i &PROC='validateMiscCreditorCheckForm'
              &PARAM="(input bSupplier.pv_ckfrm,
                       input bSupplier.pv_misc_cr)"
     }
     /* Validate the suppliers's price list. */
     {pxrun.i &PROC='validatePriceList' &PROGRAM='ppplxr.p'
               &HANDLE=ph_ppplxr
              &PARAM="(input bSupplier.pv_pr_list2,
                       input bSupplier.pv_curr,
                       input no,   /* Price List Required ? */
                       input no,   /* Do not return warning messageNumber */
                       input {&LIST-TYPE},
                       output invalidListFlag,
                       output messageNumber)"
     }
     if invalidListFlag then do:
        {pxmsg.i &MSGNUM=messageNumber
                 &ERRORLEVEL={&WARNING-RESULT}
                 &FIELDNAME = 'pv_mstr.pv_pr_list2'}
     end.
     /* Validate the suppliers's discount price list. */
     {pxrun.i &PROC='validatePriceList' &PROGRAM='ppplxr.p'
               &HANDLE=ph_ppplxr
              &PARAM="(input bSupplier.pv_pr_list,
                       input bSupplier.pv_curr,
                       input no,  /* Price List Required ? */
                       input no,  /* Do not return warning messageNumber */
                       input {&DISCOUNT-TYPE},
                       output invalidListFlag,
                       output messageNumber)"
     }
     if invalidListFlag then do:
         {pxmsg.i &MSGNUM=messageNumber
                  &ERRORLEVEL={&WARNING-RESULT}
                  &FIELDNAME = 'pv_mstr.pv_pr_list'}
     end.
     {pxrun.i &PROC='validatePriceReduction'
              &PARAM="(input bSupplier.pv_tp_pct,
                       input bSupplier.pv_tp_use_pct)"
     }
     {pxrun.i &PROC='validatePriceReductionLimit'
              &PARAM="(input bSupplier.pv_tp_pct)"
              &FILED-LIST=pv_tp_pct
     }
     {pxrun.i &PROC='validateCreditTerms' &PROGRAM='adcrxr.p'
               &HANDLE=ph_adcrxr
              &PARAM="(input bSupplier.pv_cr_terms)"
              &FIELD-LIST=pv_cr_terms
     }
  end.
  return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE setName :
/*---------------------------------------------------------------------------
   Purpose:     Assigns the name to the Address buffer
   Exceptions:  NONE
   Notes:
   History:
---------------------------------------------------------------------------*/
   define input parameter pSupplierId as character no-undo.
   define input parameter pName as character no-undo.
   define buffer bSupplier for pv_mstr.
   do on error undo, return error {&GENERAL-APP-EXCEPT}:
     {pxrun.i &PROC='processRead'
            &PARAM="(input pSupplierId,
                     buffer bSupplier,
                     input {&LOCK_FLAG},
                     input {&NO_WAIT_FLAG})"
     }
     if bSupplier.pv_sort = "" then
       assign pv_sort = pName.
     {pxrun.i &PROC='setName' &PROGRAM='adadxr.p'
               &HANDLE=ph_adadxr
              &PARAM="(input pSupplierId,
                       input pName)"
     }
   end. /* do on error undo, return error {&GENERAL-APP-EXCEPT}: */
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE generateNextId:
/*---------------------------------------------------------------------------
   Purpose:     Generates a supplier number for use
   Exceptions:  NONE
   Notes:This proc can only be used in API mode and never in character mode
   History:
---------------------------------------------------------------------------*/
   define output parameter pSupplierNbr as character no-undo.
   define variable myAppServer as handle no-undo.
   do on error undo, return error return-value:
     /* connect to the named appserver */
     {pxascon.i &APPSERVER='mfgpro' &MULTIDB=global_db &HANDLE=myAppServer}
     /* execute the adadgen.p in the new appserver*/
     {pxasrun.i &APPSERVER=myAppServer
                &PROGRAM='adsugen.p'
                &PARAM="(input global_user_lang_dir,
                         output pSupplierNbr)"
     }
     /* disconnect from the appserver*/
     {pxasdisc.i &HANDLE=myAppServer}
   end. /* do on error undo, return error {&GENERAL-APP-EXCEPT}: */
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* generateNextId */
/*============================================================================*/
PROCEDURE validateType :
/*------------------------------------------------------------------------------
  Purpose:     Validate pv_mstr.pv_type
  Exceptions:  NONE
  Notes:
  History:
------------------------------------------------------------------------------*/
   define input parameter pValue as character no-undo.
   do on error undo, return error return-value:
      {pxrun.i &PROC='validateGeneralizedCodes' &PROGRAM='gpcodxr.p'
                &HANDLE=ph_gpcodxr
         &PARAM="(input 'pv_type' ,
                  input '' ,
                  input pValue ,
                  input ''
                 )"
      }
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE validateLanguage :
/*------------------------------------------------------------------------------
  Purpose:     Validate pv_mstr.pv_lang
  Exceptions:  NONE
  Notes:
  History:
------------------------------------------------------------------------------*/
   define input parameter pValue as character no-undo.
   do on error undo, return error return-value:
      {pxrun.i &PROC='validateLanguageCode' &PROGRAM='gplngxr.p'
                &HANDLE=ph_gplngxr
         &PARAM="(input 'pv_lang' ,
                  input pValue ,
                  input yes ,
                  input ''
                 )"
      }
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE validateShipVia :
/*------------------------------------------------------------------------------
  Purpose:     Validate pv_mstr.pv_shipvia
  Exceptions:  NONE
  Notes:
  History:
------------------------------------------------------------------------------*/
   define input parameter pValue as character no-undo.
   do on error undo, return error return-value:
      {pxrun.i &PROC='validateGeneralizedCodes' &PROGRAM='gpcodxr.p'
                &HANDLE=ph_gpcodxr
         &PARAM="(input 'pv_shipvia' ,
                  input '' ,
                  input pValue ,
                  input ''
                 )"
      }
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE validateBuyer :
/*------------------------------------------------------------------------------
  Purpose:     Validate pv_mstr.pv_buyer
  Exceptions:  NONE
  Notes:
  History:
------------------------------------------------------------------------------*/
   define input parameter pValue as character no-undo.
   do on error undo, return error return-value:
      {pxrun.i &PROC='validateGeneralizedCodes' &PROGRAM='gpcodxr.p'
                &HANDLE=ph_gpcodxr
         &PARAM="(input 'pv_buyer' ,
                  input 'po_buyer' ,
                  input pValue ,
                  input ''
                 )"
      }
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE validateUser1 :
/*------------------------------------------------------------------------------
  Purpose:     Validate pv_mstr.pv_user1
  Exceptions:  NONE
  Notes:
  History:
------------------------------------------------------------------------------*/
   define input parameter pValue as character no-undo.
   do on error undo, return error return-value:
      {pxrun.i &PROC='validateGeneralizedCodes' &PROGRAM='gpcodxr.p'
                &HANDLE=ph_gpcodxr
         &PARAM="(input 'pv_user1' ,
                  input '' ,
                  input pValue ,
                  input ''
                 )"
      }
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
/*============================================================================*/
PROCEDURE validateUser2 :
/*------------------------------------------------------------------------------
  Purpose:     Validate pv_mstr.pv_user1
  Exceptions:  NONE
  Notes:
  History:
------------------------------------------------------------------------------*/
   define input parameter pValue as character no-undo.
   do on error undo, return error return-value:
      {pxrun.i &PROC='validateGeneralizedCodes' &PROGRAM='gpcodxr.p'
                &HANDLE=ph_gpcodxr
         &PARAM="(input 'pv_user2' ,
                  input '' ,
                  input pValue ,
                  input ''
                 )"
      }
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE.
      
